package com.itheima.dao;

import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentTest01 {
   @Test
   public void selectALL() throws IOException {
      //1.加载核心配置文件
      InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

      SqlSession sqlSession = sqlSessionFactory.openSession();

      List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

      for (Student stu : list) {
         System.out.println(stu);
      }
      sqlSession.close();
      is.close();
      //2.获取SqlSession工厂对象
      //3.通过工厂对象获取SqlSession对象

      //4.执行映射配置文件中的sql语句，并接收结果

      //5.提交事务

      //6.处理结果

      //7.释放资源
   }
   @Test
   public  void selectById() throws IOException {
      InputStream is = Resources.getResourceAsStream("StudentMapper.selectById");
      SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
      SqlSession sqlSession = sessionFactory.openSession();
      Student o = sqlSession.selectOne("id", 2);
      System.out.println(o);

   }

}
